package com.sinux.generality.provider.demo.controller;


import com.alibaba.fastjson.JSONObject;
import com.sinux.generality.provider.demo.tool.WebSocket;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
@RefreshScope
@RestController
@Api(tags = "openai接口")
@RequestMapping("/websokcet")
public class WebSocketController {
    private WebSocket websocket = new WebSocket();

    @RequestMapping("/connect")
    public void sendMsg(HttpServletRequest request, HttpServletResponse response) throws IOException {
        JSONObject json = new JSONObject();
        json.put("to", request.getSession().getId());
        json.put("msg", "欢迎连接WebSocket！！！！");
        websocket.onMessage(json.toJSONString());
    }
}